﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : IAward
    {
        public DataSet Award_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Award_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet Award_GetOne(int AwardID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Award_GetOneForReport", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Award_ID", AwardID);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Award_GetAllByEmployee(int employeeid)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Award_GetAllByEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", employeeid);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        
        public DataSet Award_GetList(out int ItemCount, int currentPage, int pageSize, int BranchID, int DepartmentID, string keyword, int _year)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Award_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            dbCmd.Parameters.AddWithValue("@BranchID", BranchID);
            dbCmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);
            dbCmd.Parameters.AddWithValue("@keyword", keyword);
            dbCmd.Parameters.AddWithValue("@year", _year);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool Award_CheckExistEmployee(int EmployeeID, int month, int year)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Award_CheckEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);
            try
            {
                dbConn.Open();
                int Count = Convert.ToInt32(dbCmd.ExecuteScalar());
                retVal = Count > 0 ? true : false;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public AwardInfo Award_GetInfo(int AwardID)
        {
            AwardInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Award_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Award_ID", AwardID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new AwardInfo();
                    retVal.ID = Convert.ToInt32(dr["Award_ID"]);
                    retVal.Detail = dr["Award_Detail"].ToString();
                    retVal.Date = Convert.ToDateTime(dr["Award_Date"]);
                    retVal.Type = Convert.ToString(dr["Award_Type"]);
                    retVal.TypeContent = dr["Award_TypeContent"].ToString();
                    retVal.EmployeeID = Convert.ToInt32(dr["Award_EmployeeID"]);
                    retVal.Amount = Convert.ToDecimal(dr["Award_Amount"]);
                    retVal.No = Convert.ToString(dr["Award_No"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public AwardInfo Award_GetInfoEmployee(int EmployeeID)
        {
            AwardInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Award_GetInfoEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new AwardInfo();
                    retVal.ID = Convert.ToInt32(dr["Award_ID"]);
                    retVal.Detail = dr["Award_Detail"].ToString();
                    retVal.Date = Convert.ToDateTime(dr["Award_Date"]);
                    retVal.Type = Convert.ToString(dr["Award_Type"]);
                    retVal.TypeContent = dr["Award_TypeContent"].ToString();
                    retVal.EmployeeID = Convert.ToInt32(dr["Award_EmployeeID"]);
                    retVal.Amount = Convert.ToDecimal(dr["Award_Amount"]);
                    retVal.No = Convert.ToString(dr["Award_No"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int Award_Insert(AwardInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Award_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            
            dbCmd.Parameters.AddWithValue("@Award_Date", eInfo.Date);
            dbCmd.Parameters.AddWithValue("@Award_TypeContent", eInfo.TypeContent);
            dbCmd.Parameters.AddWithValue("@Award_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@Award_Type", eInfo.Type);
            dbCmd.Parameters.AddWithValue("@Award_Detail", eInfo.Detail);
            dbCmd.Parameters.AddWithValue("@Award_Amount", eInfo.Amount);
            dbCmd.Parameters.AddWithValue("@Award_No", eInfo.No);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        
        public bool Award_Update(AwardInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Award_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Award_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@Award_Date", eInfo.Date);
            dbCmd.Parameters.AddWithValue("@Award_TypeContent", eInfo.TypeContent);
            dbCmd.Parameters.AddWithValue("@Award_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@Award_Type", eInfo.Type);
            dbCmd.Parameters.AddWithValue("@Award_Detail", eInfo.Detail);
            dbCmd.Parameters.AddWithValue("@Award_Amount", eInfo.Amount);
            dbCmd.Parameters.AddWithValue("@Award_No", eInfo.No);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet AwardExp_GetByAwardID(int AwardID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_AwardExp_GetByAwardID", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@AwardID", AwardID);          
           
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal); 
            }
            finally
            {                
                dbConn.Close();
            }
            return retVal;
        }


        public int AwardExp_Insert(AwardExpInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_AwardExp_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;

            dbCmd.Parameters.AddWithValue("@AwardID", eInfo.AwardID);
            dbCmd.Parameters.AddWithValue("@MisconductID", eInfo.MisconductID);

            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public AwardExpInfo AwardExp_GetInfo(int AwardID, int MisconductID)
        {
            AwardExpInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_AwardExp_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@AwardID", AwardID);
            dbCmd.Parameters.AddWithValue("@MisconductID", MisconductID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new AwardExpInfo();
                    retVal.ID = Convert.ToInt32(dr["AwardExp_ID"]);
                    retVal.MisconductID = Convert.ToInt32(dr["MisconductID"]);
                    retVal.AwardID = Convert.ToInt32(dr["AwardID"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public bool AwardExp_Delete(int AwardID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_AwardExp_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@AwardID", AwardID);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool Award_Delete(int AwardID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Award_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Award_ID", AwardID);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}

